package com.cjst.controller.api.sys;

import java.math.BigDecimal;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cjst.pojo.sys.VersionUpdate;
import com.cjst.service.sys.VersionUpdateService;
import com.cjst.sql.C;
import com.cjst.sql.Method;
import com.cjst.sql.WherePrams;
import com.cjst.util.common.ResultCode;
import com.cjst.util.json.ResultObj;

@Controller
@RequestMapping(value = "/api/version")
public class VersionController {
	private Logger log = Logger.getLogger(VersionController.class);

	@Autowired
	private VersionUpdateService versionUpdateService;

	@ResponseBody
	@RequestMapping(value = "/checkVersion", method = RequestMethod.POST)
	public String checkVersion(String versionId) {

		if ("".equals(versionId) || versionId == null) {
			ResultObj obj = new ResultObj(ResultCode.RESULT_EXCEPTION, "error versionId", null);
			return obj.toString();
		}

		BigDecimal clientVersion = null;
		try {
			clientVersion = new BigDecimal(versionId);
		} catch (NumberFormatException e) {
			ResultObj obj = new ResultObj(ResultCode.RESULT_EXCEPTION, "error versionId", null);
			return obj.toString();
		}

		WherePrams wherePrams = Method.where("is_delete", C.EQ, 0);
		wherePrams.orderBy(" version_code ");
		List<VersionUpdate> versionUpdates = versionUpdateService.listPage(wherePrams, " limit 0,1");

		if (versionUpdates.size() == 0 || versionUpdates == null) {
			ResultObj obj = new ResultObj(ResultCode.RESULT_EXCEPTION, "failed", null);
			return obj.toString();
		}

		else if (versionUpdates.size() == 1) {
//			BigDecimal currentVersion = versionUpdates.get(0).getVersionCode();
//
//			switch (currentVersion.compareTo(clientVersion)) {
//			case -1: {
//				ResultObj obj = new ResultObj(ResultCode.RESULT_EXCEPTION, "failed", null);
//				return obj.toString();
//			}
//
//			case 0: {
//				ResultObj obj = new ResultObj(ResultCode.RESULT_EXCEPTION, "It's the lastest version", null);
//				return obj.toString();
//			}
//
//			case 1: {
//				ResultObj obj = new ResultObj(ResultCode.RESULT_EXCEPTION, "success", versionUpdates.get(0));
//				return obj.toString();
//			}
//
//			}

		}

		return null;
	}
	
	@ResponseBody
	@RequestMapping(value = "/lastestVersion", method = RequestMethod.POST)
	public String lastestVersion() {



		WherePrams wherePrams = Method.where("isDelete", C.EQ, 0);
		wherePrams.orderBy(" version_code ");
		List<VersionUpdate> versionUpdates = versionUpdateService.listPage(wherePrams, " limit 0,1");

		if (versionUpdates == null||versionUpdates.size() == 0  ) {
			ResultObj obj = new ResultObj(ResultCode.RESULT_EXCEPTION, "failed", null);
			return obj.toString();
		}

		else if (versionUpdates.size() == 1) {
			ResultObj obj = new ResultObj(ResultCode.RESULT_SUCCESS, "success", versionUpdates.get(0));
			return obj.toString();
		} 

		return "error";
	}

}
